<?php

/**
 * Copyright (C) 2020 Tencent Cloud.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace App\Install\Controller;

use App\Settings\SettingsRepository;
use Discuz\Web\Controller\AbstractWebController;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\View\Factory;
use Psr\Http\Message\ServerRequestInterface;

class IndexController extends AbstractWebController
{
    public function render(ServerRequestInterface $request, Factory $view)
    {
        $url = $this->app->make(UrlGenerator::class);

        $viewPath = 'install.problems';
        $problems = $this->preRequireSite();
        if (!$problems->count()) {
            $viewPath = 'install.install';
        }

        $data = [
            'title' => 'Discuz! Q 安装程序',
            'problems' => $problems,
            'logo' => ''
        ];

        return $view->make($viewPath)->with($data);
    }

    private function preRequireSite()
    {
        $problems = collect();
        $defaultPhpVersion = '7.2.0';
        $phpversion = version_compare(PHP_VERSION, $defaultPhpVersion, '>=');
        if (!$phpversion) {
            $problems->push(['message' => 'PHP 版本必须大于'.$defaultPhpVersion]);
        }

        $extension = collect([
            'dom',
            'gd',
            'json',
            'mbstring',
            'openssl',
            'pdo_mysql',
            'fileinfo'
        ])->reject(function ($extension) {
            return extension_loaded($extension);
        })->map(function ($extension) {
            return [
                'message' => "PHP扩展 '$extension' 未安装.",
            ];
        });

        $problems = $problems->merge($extension);

        $storageDirWritable = is_writable(base_path('storage'));
        $cacheDirWritable = is_writable(storage_path('cache'));
        $configDirWritable = is_writable($this->app->configPath());

        if (!$storageDirWritable) {
            $problems->push(['message' => 'storage 目录不可写']);
        }

        if (!$cacheDirWritable) {
            $problems->push(['message' => 'storage/cache 目录不可写']);
        }

        if (!$configDirWritable) {
            $problems->push(['message' => 'config 目录不可写']);
        }

        if ($this->app->isInstall()) {
            $url = $this->app->make(UrlGenerator::class);
            $setting = $this->app->make(SettingsRepository::class);
            $problems->push(['message' => '您已安装过站点，请直接访问 <a href="'.$url->to('/').'">'.$setting->get('site_name').'</a>']);
        }

        return $problems;
    }
}
